package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.*;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.human.service.IParticipantService;
import com.ruoyi.human.service.ITrialDataRecordHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.service.ITrialDataRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验数据记录Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/record")
public class TrialDataRecordController extends BaseController
{
    @Autowired
    private ITrialDataRecordService trialDataRecordService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    @Autowired
    private ITrialDataRecordHistoryService trialDataRecordHistoryService;
    /**
     * 查询实验数据记录列表
     */
    @RequiresPermissions("human:record:list")
    @GetMapping("/list")
    public TableDataInfo list(TrialDataRecord trialDataRecord)
    {
        startPage();
        List<TrialDataRecord> list = trialDataRecordService.selectTrialDataRecordList(trialDataRecord);
        for (TrialDataRecord trialDataRecord1:list){
            Long trialId=trialDataRecord1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                trialDataRecord1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        for (TrialDataRecord trialDataRecord1:list){
            Long participantId=trialDataRecord1.getParticipantId();
            Participant participant=participantService.selectParticipantByParticipantId(participantId);
            if(participant!=null){
                trialDataRecord1.setParticipantName(participant.getParticipantName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出实验数据记录列表
     */
    @RequiresPermissions("human:record:export")
    @Log(title = "实验数据记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrialDataRecord trialDataRecord)
    {
        List<TrialDataRecord> list = trialDataRecordService.selectTrialDataRecordList(trialDataRecord);
        ExcelUtil<TrialDataRecord> util = new ExcelUtil<TrialDataRecord>(TrialDataRecord.class);
        util.exportExcel(response, list, "实验数据记录数据");
    }

    /**
     * 获取实验数据记录详细信息
     */
    @RequiresPermissions("human:record:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(trialDataRecordService.selectTrialDataRecordByDataId(dataId));
    }

    /**
     * 新增实验数据记录
     */
    @RequiresPermissions("human:record:add")
    @Log(title = "实验数据记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrialDataRecord trialDataRecord)
    {
        trialDataRecord.setCreatedBy(SecurityUtils.getUsername());
        trialDataRecord.setCreatedAt(new Date());
        trialDataRecordService.insertTrialDataRecord(trialDataRecord);
        TrialDataRecordHistory trialDataRecordHistory=new TrialDataRecordHistory();
        BeanUtils.copyProperties(trialDataRecord,trialDataRecordHistory);
        trialDataRecordHistoryService.insertTrialDataRecordHistory(trialDataRecordHistory);
        return toAjax(true);
    }

    /**
     * 修改实验数据记录
     */
    @RequiresPermissions("human:record:edit")
    @Log(title = "实验数据记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrialDataRecord trialDataRecord)
    {
        trialDataRecord.setUpdatedBy(SecurityUtils.getUsername());
        trialDataRecord.setUpdatedAt(new Date());
        trialDataRecord.setVersion(trialDataRecord.getVersion()+1);
        trialDataRecordService.updateTrialDataRecord(trialDataRecord);
        TrialDataRecordHistory trialDataRecordHistory=new TrialDataRecordHistory();
        BeanUtils.copyProperties(trialDataRecord,trialDataRecordHistory);
        trialDataRecordHistoryService.insertTrialDataRecordHistory(trialDataRecordHistory);
        return toAjax(true);
    }

    /**
     * 删除实验数据记录
     */
    @RequiresPermissions("human:record:remove")
    @Log(title = "实验数据记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        for (Long id:dataIds){
            TrialDataRecord trialDataRecord=trialDataRecordService.selectTrialDataRecordByDataId(id);
            trialDataRecord.setDeletedAt(new Date());
            trialDataRecord.setDeletedBy(SecurityUtils.getUsername());
            trialDataRecord.setDeletedFlag(1);
            trialDataRecordService.updateTrialDataRecord(trialDataRecord);
        }
        return toAjax(true);
    }
}
